<?php
header("Content-type: text/html; charset=utf-8");
/**
 * 抽象主体角色
 * @author fld
 *
 */
interface Subject{
	/**
	 * 添加观察者
	 * @param ObServer $obs
	 */
	public function add(ObServer $obs);
	/**
	 * 删除guancz
	 * @param ObServer $obs
	 */
	public function delete(ObServer $obs);
	/**
	 * 通知事件
	 */
	public function notify();
}
/**
 * 抽象观察者角色
 * @author fld
 *
 */
interface ObServer{
	public function send();
}
/**
 * 具体观察者1
 * @author fld
 *
 */
class ConcretedObserver1 implements ObServer{
	public function send(){
		echo "观察者1收到通知发送信息<br/>";
	}
}
/**
 * 具体观察者2
 * @author fld
 *
 */
class ConcretedObserver2 implements ObServer{
	public function send(){
		echo "观察者2收到通知发送信息<br/><br/><br/>";
	}
}
/**
 * 具体主体角色
 * @author Administrator
 *
 */
class ConcreteSubject implements Subject{
	private $ObServers = [];
	/**
	 * 添加观察者
	 * @param ObServer $obs
	 */
	public function add(ObServer $obs){
		$this->ObServers[] = $obs;
	}
	/**
	 * 删除观察者
	 * @param ObServer $obs
	 */
	public function delete(ObServer $obs){
		foreach ($this->ObServers as $k=>$observer){
			if(serialize($observer) === serialize($obs)){
				unset($this->ObServers[$k]);
			}
		}
	}
	/**
	 * 事件通知
	 */
	public function notify(){
		foreach ($this->ObServers as $obs){
			$obs->send();
		}
	}
}

$subs = new ConcreteSubject();
$subs->add(new ConcretedObserver1());
$subs->add(new ConcretedObserver2());
$subs->notify();
$subs->delete(new ConcretedObserver2());
$subs->notify();